import java.util.Calendar;
import java.util.Vector;

import javax.swing.DefaultListModel;


public class Alert {
	private Calendar today;
	private DefaultListModel<String> alertList;
	
	public Alert(){
		today=Calendar.getInstance();
		alertList = new DefaultListModel<String>();
	}

	/*This method calculates the number of months of bills that has not been paid.
	 * 
	 * if it returns 0, means no months of bill being calculated is owed.
	 * if positive, the number of months calculated include this month's
	 * if negative, the number of months owed calculated not include this month's
	 * 
	 */	
	public int calcDiffMonths(Calendar nextPayDate){

		int diffMonths=0;
		int diffYears=0;

		//System.out.println(today.get(Calendar.YEAR) + "/" + today.get(Calendar.MONTH) + "/" + today.get(Calendar.DAY_OF_MONTH) + "");

		//current month's is paid
		if(today.get(Calendar.YEAR) ==  nextPayDate.get(Calendar.YEAR) &&
				today.get(Calendar.MONTH) < nextPayDate.get(Calendar.MONTH)){ 

			return 0;	
		}

		//ONLY current month's not paid
		else if( today.get(Calendar.YEAR) ==  nextPayDate.get(Calendar.YEAR) &&
				today.get(Calendar.MONTH) == nextPayDate.get(Calendar.MONTH) 
				&& (today.get(Calendar.DAY_OF_MONTH) >= nextPayDate.get(Calendar.DAY_OF_MONTH)) ){
			return 1;
		}

		//>=1 previous months not paid (and possibly this month's one also) 
		else if( today.get(Calendar.YEAR) ==  nextPayDate.get(Calendar.YEAR)){ //same year
			diffMonths=today.get(Calendar.MONTH) - nextPayDate.get(Calendar.MONTH);
			if ((today.get(Calendar.DAY_OF_MONTH) >= nextPayDate.get(Calendar.DAY_OF_MONTH)) )
				diffMonths++;
			else diffMonths *=-1;

			return diffMonths;
		}
		else if( (diffYears=today.get(Calendar.YEAR) - nextPayDate.get(Calendar.YEAR)) >=1){
			diffMonths=today.get(Calendar.MONTH) - nextPayDate.get(Calendar.MONTH);
			if(diffMonths == 0) diffMonths= 12*diffYears;
			else if(diffMonths<0) diffMonths= (12 * (diffYears-1) ) + (12+diffMonths);
			else diffMonths=12 * diffYears + diffMonths;

			if ((today.get(Calendar.DAY_OF_MONTH) >= nextPayDate.get(Calendar.DAY_OF_MONTH)) )
				diffMonths++;
			else diffMonths *=-1;

			return diffMonths;

		}

		return diffMonths;
	}
	
	public void checkBill(Bill bill){
		Calendar N=bill.getNextPayDate();
		String desc=bill.getDescription();
		
		int diffMonths=calcDiffMonths(N);
	
		if (diffMonths==0);

		else if(diffMonths==1) alertList.addElement("This month's "+desc+" is due!") ;
		else if(diffMonths==-1)alertList.addElement("Last month's "+desc+" is owed!") ;

		else if(diffMonths<-1)alertList.addElement(-diffMonths+" months of "+desc+" is owed!") ;
		else alertList.addElement(diffMonths+" months of "+desc+" is owed!") ;
	}

	/**
	 * Each time the salary or a bill is paid,
	 * this function is called to recalculate and generate all the alerts
	 */
	
	public void generateAlerts(Vector<Bill> billList){
		alertList.clear();
			
		Bill bill;
		for(int i=0;i<billList.size();i++){
			bill=billList.get(i);
			checkBill(bill);
		}
		
		if(alertList.size()==0) alertList.addElement("No alerts currently!");

	}
	
	public void add(String string){
		alertList.addElement(string);
		
	}
	
	public void print(){
		System.out.println("================");
		System.out.println("Alerts");
		
		for(int i=0;i<alertList.size();i++){
			System.out.println(alertList.get(i));
		}
		
		System.out.println("================");
	}

	public DefaultListModel<String> getAlertList() {
		return alertList;
	}

	public void setAlertList(DefaultListModel<String> alertList) {
		this.alertList = alertList;
	}

}
